<?php

/*
 * This is my first OOP project. This is a test project!
 * The name of project is Bookstore. 
 */

/**
 * This class is handling information about books
 * @author Velislav Belev
 * @license   http://opensource.org/licenses/gpl-3.0.html GNU Public License
 */

namespace Models;
class Books extends \Models\User {
    private $_allBooks;
    private $_bookTitle;

    private function _setAllBooks($page) {
        $start=0+$page*10;
        $stop=10+$page*10;
        $this->_allBooks = $this->query("select * from(select * from (SELECT * FROM (select * from carti order by titlu asc)  as b INNER JOIN
    legautorcarte as ba ON b.id_carte=ba.cod_carte INNER JOIN autori as a
     ON a.id_autor=ba.cod_autor ) as T1 ORDER BY T1.titlu ASC) as T2 limit ".$start.",".$stop);
    }

    /**
     * This method returns all books from DB.
     * @return array
     */
    public function getAllBooks($page) {
        $this->_setAllBooks($page);
        //return array_slice($this->_allBooks,0,20);
        return $this->_allBooks;
        
    }
       
    private function _setIsBookTitleValid($bookTitle) {
        $bookTitle = trim($bookTitle);
         if (mb_strlen($bookTitle) < 2) {
            throw new \Exception('Invalid book title', 104);
        }
        $this->_bookTitle=$bookTitle;
    }
    
    private function _getIsBookTitleValid() {
        return $this->_bookTitle;
    }
    
    /**
     * This method saves new book in DB. It validates Book Title, 
     * checks if author IDs are in DB and save info.
     * @param int $isAdmin - 1 is for registrated users, 2 is for admins
     * @param string $bookTitle - Book Title to save
     * @param int, array of int $authors - Authors IDs to add to book
     * @throws \Exception
     */
    public function saveBook($isAdmin ,$bookTitle, $authors) {
        if($isAdmin != 2){
            throw new \Exception('Only administrator can enter new books', 126); 
        }
        $bookTitle = $this->escape($bookTitle);             
        $this->_setIsBookTitleValid($bookTitle);
        $this->_getIsBookTitleValid();    
        
        $isBookInDb = $this->isInDb('books', 'book_title', $bookTitle);
        if($isBookInDb != 0) {
            throw new \Exception('This book title already exists in the DB', 113);
        }

        $isIdInDb = $this->isInDb('authors', 'author_id', $authors);
        if($isIdInDb == count($authors)) {
            $insertData = array('book_title' => $bookTitle);
            $this->insert('books', $insertData);
            $bookId = $this->getInsertId();

            foreach ($authors as $authorId) {
                $insertData = array('book_id' => $bookId, 'author_id' => $authorId);
              $isSave= $this->insert('books_authors', $insertData);            
            }
           if($isSave == 0){
               echo 'Saved';
           } else {
               throw new \Exception('Error in saving.', 106);
           }           

        } else {
            throw new \Exception('Invalid author name', 107);
        }              
    }
    
    /**
     * By this method I check if book id is valid. 
     * If everything is OK it gets all users comments for selected book. 
     * It is used in book.php
     * @param int $bookId
     * @return array
     */
    public function getSelectedBook($bookId) {
        $bookId = (int)$bookId;
        $isBookIdExist = $this->isInDb('books', 'book_id', $bookId);
        if($isBookIdExist == 1) {
           $bookId = $this->escape($bookId); 
           return $queryResult = $this->query('SELECT * FROM books_authors as ba
                LEFT JOIN books as b ON ba.book_id = b.book_id
                LEFT JOIN authors as a ON ba.author_id = a.author_id
                LEFT JOIN users_comments as uc ON uc.book_id = b.book_id
                LEFT JOIN users as u ON uc.user_id=u.user_id
                LEFT JOIN books_authors as ba2 ON ba2.book_id=ba.book_id
                WHERE ba2.book_id = '.$bookId.'
                            ORDER BY uc.date DESC');       
        } else {
             header('Location: index.php');
        }                    
    } 
        
    
    
}
